#!/usr/bin/env python
# encoding: utf-8

from app.views.api import route_api
from  flask import request,jsonify,g
import requests,json,decimal

from app import db
from app.model import User
from app.model import Project

from app.common.libs.Logging import logger
from app.common.libs.Helper import getCurrentDate, getFormatDate, stringToDatetime
from app.common.libs.UserService import UserService

from sqlalchemy import func, desc

@route_api.route("/project/search",methods = [ "GET","POST" ])
def projectSearch():
    resp = { 'code':20000,'message':'操作成功~', 'data':{}}
    req = request.values
    name = req['name'] if 'name' in req else ''
    page = int( req['page'] ) if 'page' in req else 0
    limit = int( req['limit'] ) if 'limit' in req else 0
    offset = ( page - 1 ) * limit
    total = db.session.query( Project ).count()
    project_list = db.session.query( Project ).offset( offset ).limit( limit ).all()

    resp['data']['items'] = [{
                'id':item.id,
                'name':item.name,
                'created_time':getFormatDate(item.created_time),
            } for item in project_list ]

    resp['data']['total'] = total

    return jsonify( resp )

@route_api.route("/project/delete",methods = [ "GET","POST" ])
def projectDelete():
    resp = { 'code':20000,'message':'删除成功~', 'data':{}}
    req = request.values
    id = int(req['id']) if 'id' in req else 0
    if id < 1:
        resp['code'] = -1
        resp['message'] = '删除错误，项目不存在~'
        return jsonify( resp )
    Project.query.filter_by(id = id).delete()
    db.session.commit()
    return jsonify( resp )

@route_api.route("/project/edit",methods = [ "GET","POST" ])
def projectEdit():
    resp = { 'code':20000,'message':'编辑成功~', 'data':{}}
    req = request.values
    id = req['id'] if 'id' in req else ''
    name = req['name'] if 'name' in req else ''
    if not name:
        resp['code'] = -1
        resp['message'] = '请填写项目名称~'
        return jsonify( resp )

    user = req['user'] if 'user' in req else ''
    if not user:
        resp['code'] = -1
        resp['message'] = '请填写项目负责人~'
        return jsonify( resp )

    project_info = Project.query.filter_by(id = id).first()
    project_info.name = name
    project_info.user = user
    project_info.updated_time = getCurrentDate()
    db.session.commit()

    return jsonify( resp )

@route_api.route("/project/add",methods = [ "GET","POST" ])
def projectAdd():
    resp = { 'code':20000,'message':'添加成功~', 'data':{}}
    req = request.values
    name = req['name'] if 'name' in req else ''
    if not name:
        resp['code'] = -1
        resp['message'] = '请填写项目名称~'
        return jsonify( resp )

    project_info = Project.query.filter_by(name = name).first()
    if project_info:
        resp['code'] = -1
        resp['message'] = '项目已存在~'
        return jsonify( resp )

    user = req['user'] if 'user' in req else ''
    if not user:
        resp['code'] = -1
        resp['message'] = '请填写项目负责人~'
        return jsonify( resp )

    project_model = Project()
    project_model.user = user
    project_model.name = name
    project_model.created_time = getCurrentDate()
    project_model.updated_time = getCurrentDate()

    db.session.add(project_model)
    db.session.commit()
    return jsonify( resp )

@route_api.route("/project/list",methods = [ "GET","POST" ])
def projectList():
    resp = { 'code':20000,'message':'操作成功~', 'data':{}}
    req = request.values
    keywords = req['keywords'] if 'keywords' in req else ''
    page = int( req['page'] ) if 'page' in req else 0
    limit = int( req['limit'] ) if 'limit' in req else 0
    offset = ( page - 1 ) * limit

    total = db.session.query( Project )\
        .filter(Project.name.like('%'+keywords+'%')).count()
    project_list = db.session.query( Project )\
        .filter(Project.name.like('%'+keywords+'%'))\
        .offset( offset ).limit( limit ).all()

    resp['data']['items'] = [{
                'id':item.id,
                'name':item.name,
                'user':item.user,
                'created_time':getFormatDate(item.created_time),
            } for item in project_list ]

    resp['data']['total'] = total

    return jsonify( resp )

@route_api.route("/project/all",methods = [ "GET","POST" ])
def projectAll():
    resp = { 'code':20000,'message':'操作成功~', 'data':{}}
    req = request.values
    project_list = db.session.query( Project ).all()

    resp['data']['items'] = [{
                'id':item.id,
                'name':item.name,
                'user':item.user,
                'created_time':getFormatDate(item.created_time),
            } for item in project_list ]

    return jsonify( resp )

